package com.example.xyr.myapplication.fragment

import android.os.Bundle
import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.widget.GridView
import android.widget.LinearLayout
import com.dev.sacot41.scviewpager.SCViewPager
import com.example.xyr.myapplication.Constant
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.adapter.EmotionGridViewAdapter
import com.example.xyr.myapplication.adapter.EmotionPagerAdapter
import com.example.xyr.myapplication.base.BaseFragment
import com.example.xyr.myapplication.utils.DisplayUtils
import com.example.xyr.myapplication.utils.EmotionUtils
import com.example.xyr.myapplication.utils.GlobalOnItemClickManagerUtils
import com.example.xyr.myapplication.view.EmojiIndicatorView
import java.util.*



/**
 * Created by ylcf on 2018/9/6.
 */
class EmoFragment : BaseFragment() {

    private lateinit var vp_complate_emotion_layout: SCViewPager
    private lateinit var ll_point_group: EmojiIndicatorView
    private var emoType: Int = 0
    private lateinit var emotionPagerGvAdapter: EmotionPagerAdapter

    override fun getLayoutId(): Int = R.layout.fragment_emo

    override fun initView(view: View, savedInstanceState: Bundle?) {
        vp_complate_emotion_layout = view.findViewById(R.id.vp_complate_emotion_layout)
        ll_point_group = view.findViewById(R.id.ll_point_group)
        emoType = savedInstanceState?.get(Constant.EMOTION_MAP_TYPE) as Int
        initEmotion()
        initListener()
    }


    private fun initListener(){
        vp_complate_emotion_layout.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            internal var oldPagerPos = 0
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
                ll_point_group.playByStartPointToNext(oldPagerPos, position)
                oldPagerPos = position
            }

            override fun onPageScrollStateChanged(state: Int) {

            }
        })

    }

    /**
     * 初始化表情面板
     * 思路：获取表情的总数，按每行存放7个表情，动态计算出每个表情所占的宽度大小（包含间距），
     * 而每个表情的高与宽应该是相等的，这里我们约定只存放3行
     * 每个面板最多存放7*3=21个表情，再减去一个删除键，即每个面板包含20个表情
     * 根据表情总数，循环创建多个容量为20的List，存放表情，对于大小不满20进行特殊
     * 处理即可。
     */
    private fun initEmotion() {
        // 获取屏幕宽度
        val screenWidth = DisplayUtils.getScreenWidthPixels(activity)
        // item的间距
        val spacing = DisplayUtils.dp2px(activity, 12F)
        // 动态计算item的宽度和高度
        val itemWidth = (screenWidth - spacing * 8) / 7
        //动态计算gridview的总高度
        val gvHeight = itemWidth * 3 + spacing * 6

        val emotionViews = ArrayList<GridView>()
        var emotionNames: MutableList<String> = ArrayList()
        // 遍历所有的表情的key
        for (emojiName in EmotionUtils.getEmojiMap(emoType).keys) {
            emotionNames.add(emojiName)
            // 每20个表情作为一组,同时添加到ViewPager对应的view集合中
            if (emotionNames.size == 20) {
                val gv = createEmotionGridView(emotionNames, screenWidth, spacing, itemWidth, gvHeight)
                emotionViews.add(gv)
                // 添加完一组表情,重新创建一个表情名字集合
                emotionNames = ArrayList()
            }
        }

        // 判断最后是否有不足20个表情的剩余情况
        val gv:GridView
        if (emotionNames.size > 0) {
            gv = createEmotionGridView(emotionNames, screenWidth, spacing, itemWidth, gvHeight)
            emotionViews.add(gv)
        }

        //初始化指示器
        ll_point_group.initIndicator(emotionViews.size)
        // 将多个GridView添加显示到ViewPager中
        emotionPagerGvAdapter = EmotionPagerAdapter(emotionViews)
        vp_complate_emotion_layout.adapter = (emotionPagerGvAdapter)
        val params = LinearLayout.LayoutParams(screenWidth, gvHeight)
        vp_complate_emotion_layout.layoutParams = params

    }


    /**
     * 创建显示表情的GridView
     */

    private fun createEmotionGridView(emotionNames: List<String>, gvWidth: Int, padding: Int, itemWidth: Int, gvHeight: Int): GridView {
        // 创建GridView
        val gv = GridView(mActivity)
        //设置点击背景透明
        gv.setSelector(android.R.color.transparent)
        //设置7列
        gv.numColumns = 7
        gv.setPadding(padding, padding, padding, padding)
        gv.horizontalSpacing = padding
        gv.verticalSpacing = padding * 2
        //设置GridView的宽高
        val params = ViewGroup.LayoutParams(gvWidth, gvHeight)
        gv.layoutParams = params
        // 给GridView设置表情图片
        val adapter = EmotionGridViewAdapter(activity!!, emotionNames, itemWidth, emoType)
        gv.adapter = adapter
        //设置全局点击事件
        gv.onItemClickListener = GlobalOnItemClickManagerUtils.getInstance(activity).getOnItemClickListener(emoType)
        return gv
    }

}